package com.leyou.page.service.impl;

import com.leyou.api.client.ItemClient;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.JsonUtils;
import com.leyou.item.dto.*;
import com.leyou.page.service.PageService;
import com.leyou.page.vo.BrandVO;
import com.leyou.page.vo.SpecGroupVO;
import com.leyou.page.vo.SpecParamVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PageServiceImpl implements PageService {


    @Autowired
    private ItemClient itemClient;
    @Autowired
    private StringRedisTemplate redisTemplate;

    private static final String KEY_PREFIX_SPU = "page:spu:id";
    private static final String KEY_PREFIX_SKU = "page:sku:id";
    private static final String KEY_PREFIX_DETAIL = "page:detial:id";
    private static final String KEY_PREFIX_CATEGORY = "page:category:id";
    private static final String KEY_PREFIX_BRAND = "page:brand:id";
    private static final String SPEC_KEY_PREFIX = "page:spec:id:";

    /*
    * redis数据新增和修改
    * */
    @Override
    public String saveBrand2Redis(BrandVO brandVO) {
        return null;
    }

    @Override
    public Boolean deleteSpuDetail(Long spuId) {
        return redisTemplate.delete(KEY_PREFIX_DETAIL + spuId);
    }

    @Override
    public Boolean deleteSpec(Long categoryId) {
        return redisTemplate.delete(SPEC_KEY_PREFIX + categoryId);
    }

    @Override
    public Boolean deleteCategories(List<Long> ids) {

        return redisTemplate.delete(KEY_PREFIX_CATEGORY+ids);
    }

    @Override
    public Boolean deteleSpu(Long id) {
        return redisTemplate.delete(KEY_PREFIX_SPU + id);
    }

    /*
    * 从redis中删除品牌信息
    * */
    @Override
    public Boolean deleteBrand(Long spuId) {

        return redisTemplate.delete(KEY_PREFIX_BRAND + spuId);
    }

    /*
    * 从redis删除商品sku信息
    * */
    @Override
    public Boolean deleteSku(Long spuId) {

        return redisTemplate.delete(KEY_PREFIX_SKU+spuId);
    }

    //   根据id查询spudata
    @Override
    public String spuLoadData(Long id) {
        //查询信息
        SpuDTO spu = itemClient.querySpuById(id);

        //    代码健壮性判断
        if(spu == null || spu.getId() == null){
            throw new LyException(404, "商品不存在");
        }
        //   组织数据
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", spu.getId());
        map.put("name", spu.getName());
        map.put("categoryIds", spu.getCategoryIds());
        map.put("brandId", spu.getBrandId());
        //  转json格式
        String json = JsonUtils.toJson(map);
        redisTemplate.opsForValue().set(KEY_PREFIX_SPU + id, json);

        return json;
    }
//    查询商品sku数据
    @Override
    public String loadSkuListData(Long spuId) {
//        查询数据
        List<SkuDTO> skuList = itemClient.querySkuBySpuId(spuId);
        if (skuList == null || skuList.size() <= 0) {
            throw new LyException(404, "商品不存在！");
        }
        String json = JsonUtils.toJson(skuList);
//        数据存入redis
        redisTemplate.opsForValue().set(KEY_PREFIX_SKU+spuId,json);
        return json;
    }
    /*
    * 查询spuDetail数据
    * */
    @Override
    public String loadSpuDatailData(Long spuId) {
//        查询数据
        SpuDetailDTO spuDatail = itemClient.querySpuDetailById(spuId);

        if (spuDatail == null) {
            throw new LyException(404, "商品不存在！");
        }

        String json = JsonUtils.toJson(spuDatail);
//        存入redis
        redisTemplate.opsForValue().set(KEY_PREFIX_DETAIL+spuDatail,json);

        return json;
    }
/*
* 查询category数据
* */
    @Override
    public String loadCategoriesData(List<Long> ids) {
//        查询数据
        List<CategoryDTO> list = itemClient.queryCategoryByIds(ids);

        if (list == null || list.size() <= 0) {
            throw new LyException(404, "商品不存在！");
        }
        List<Map<String, Object>> categoryList = list.stream().map(categoryDTO -> {
            Map<String, Object> map = new HashMap<>();
            map.put("id", categoryDTO.getId());
            map.put("name", categoryDTO.getName());
            return map;
        }).collect(Collectors.toList());
        String json = JsonUtils.toJson(categoryList);
//        存入redis       ids.get(2)获取菜单最后一级
        redisTemplate.opsForValue().set(KEY_PREFIX_CATEGORY+ids.get(2),json);

        return json;
    }
    /*
    * 根据品牌查询数据
    * */
    @Override
    public String loadBrandData(Long id) {
//        查询数据
        BrandDTO brandDTO = itemClient.queryBrandById(id);

        if (brandDTO == null) {
            throw new LyException(404, "商品不存在！");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("id", brandDTO.getId());
        map.put("name", brandDTO.getName());
//          格式转换
        String json = JsonUtils.toJson(brandDTO);
//        存入redis
        redisTemplate.opsForValue().set(KEY_PREFIX_BRAND+id,json);

        return json;
    }
/*
* 根据商品规格查询数据
*
* */
    @Override
    public String loadSpecData(Long categoryId) {

        List<SpecGroupDTO> list = itemClient.querySpecList(categoryId);

//        代码健壮性判断
        if(list == null || list.size() <= 0){
            throw new LyException(404, "商品不存在");
        }
        List<SpecGroupVO> groupList = new ArrayList<>(list.size());
        for (SpecGroupDTO specGroup : list) {
//            创建Vo
            SpecGroupVO groupVo = new SpecGroupVO();
//            放入集合
            groupList.add(groupVo);
//            填写属性
            groupVo.setName(specGroup.getName());
//            params属性
            List<SpecParamVO> params = specGroup.getParams()
                    .stream()
                    .map(paramDTO -> paramDTO.toEntity(SpecParamVO.class))
                    .collect(Collectors.toList());
            groupVo.setParams(params);
        }
//        序列化为json
        String json = JsonUtils.toJson(groupList);
//        写入redis
        redisTemplate.opsForValue().set(SPEC_KEY_PREFIX+categoryId,json);
        return json;
    }
}
